﻿package ${serviceImpl.packageName};

import org.springframework.stereotype.Service;
import cn.demoncat.util.mybatis.plus.base.BaseServiceImpl;

import ${entity.fullName};
import ${mapper.packageName}.${entity.name}${mapperSuffix};
import ${service.packageName}.${entity.name}Service;
import cn.demoncat.util.lang.entity.Result;
import org.springframework.transaction.annotation.Transactional;
<#if entity.hasName || entity.hasTitle>
import cn.demoncat.util.lang.ParamEmptyUtil;
</#if>

/** 
 * ${entity.comment}接口实现类
 *
 * @author ${entity.author}
 *
 * @since ${entity.time}
 */
@Service
public class ${entity.name}ServiceImpl extends BaseServiceImpl<${entity.name}${mapperSuffix}, ${entity.name}> implements ${entity.name}Service {

    /**
     * 添加
     *
     * @param data
     * @return
     *
     * @author ${entity.author}
     * @since ${entity.time}
     */
    @Transactional
    @Override
    public Result add(${entity.name} data) {
        // 参数校验
        <#if entity.hasName>
        ParamEmptyUtil.checkBlank(data.getName());
        <#elseif entity.hasTitle>
        ParamEmptyUtil.checkBlank(data.getTitle());
        </#if>
        // 保存
        data.setId(null);
        <#if entity.hasSort>
        data.setSort(System.currentTimeMillis());
        </#if>
        this.save(data);
        return Result.success(data);
    }

    /**
     * 修改
     *
     * @param data
     * @return
     *
     * @author ${entity.author}
     * @since ${entity.time}
     */
    @Transactional
    @Override
    public Result upd(${entity.name} data) {
        // 参数校验
        <#if entity.hasName>
        ParamEmptyUtil.checkBlank(data.getName());
        <#elseif entity.hasTitle>
        ParamEmptyUtil.checkBlank(data.getTitle());
        </#if>
        // 查询原数据
        return this.handle(data.getId(), dbData ->{
            // 保存
            <#if entity.hasSort>
            data.setSort(null);
            </#if>
            this.update(data);
            return Result.success(data);
        });
    }

    /**
     * 删除
     *
     * @param id
     * @return
     *
     * @author ${entity.author}
     * @since ${entity.time}
     */
    @Transactional
    @Override
    public Result del(${entity.idAttr.type} id) {
        // 删除
        this.delete(id);
        return Result.SUCCESS;
    }

    <#if entity.hasSort>
    /**
     * 排序
     *
     * @param id
     * @param sort
     * @return
     *
     * @author ${entity.author}
     * @since ${entity.time}
     */
    @Transactional
    @Override
    public Result sort(${entity.idAttr.type} id, Long sort) {
        // 排序
        this.updateSort(id, sort);
        return Result.SUCCESS;
    }
    </#if>
}